import React, { useCallback, useState } from 'react'
import { View } from '@tarojs/components'
import { useSelector } from 'react-redux'
import HomePopUp from '@COMPONENTS/HomePopUp/index'
import PlanFeedbackPop from '@COMPONENTS/HomePopUp/PlanFeedbackPop'
import InvitorPopUp from '@COMPONENTS/HomePopUp/InvitorPopUp'
import ReportPop from '@COMPONENTS/HomePopUp/ReportPop'
import PlanCompleteModal from '@COMPONENTS/PlanManagement/PlanCompleteModal'
import { RootStateITF } from '@REDUXITF/index'
import { ManagementPlanStateITF } from '@REDUXITF/managementPlan'
import { SHARE_ROUTE_TYPE } from '@CONSTANTS/app'

interface HomeAllPopUpProps {
  type?: string;
  planId?: string;
  planTitle?: string;
  showLogin: () => void;
  isSameAccount: boolean;
}
const HomeAllPopUp: React.FC<HomeAllPopUpProps> = ({ planId, planTitle, showLogin, type, isSameAccount }) => {
  const {
    myPainManagement, homePageSpotEvents
  } = useSelector<RootStateITF, ManagementPlanStateITF>(state => state.managementPlan)
  const { DoneForTodayFlag, HasPainManagement } = myPainManagement

  const [hiddenInvitor, setHiddenInvitor] = useState<boolean>(type !== SHARE_ROUTE_TYPE.invite || isSameAccount)
  const [hiddenFeedBack, setHiddenFeedBack] = useState<boolean>(false)
  const isLoadHomePopUp = hiddenFeedBack
  const onCloseInvitor = useCallback(() => {
    setHiddenInvitor(true)
  }, [])
  const onCloseFeedback = useCallback(() => {
    setHiddenFeedBack(true)
  }, [])
  return (
    <View>
      <ReportPop />
      {HasPainManagement && <PlanCompleteModal doneForTodayFlag={DoneForTodayFlag} />}
      {isLoadHomePopUp && <HomePopUp showLogin={showLogin} />}
      <PlanFeedbackPop
        onClose={onCloseFeedback}
        isShow={hiddenInvitor}
      />
      <InvitorPopUp
        type={type}
        isSameAccount={isSameAccount}
        planId={planId}
        planTitle={planTitle}
        showLogin={showLogin}
        homePageSpotEvents={homePageSpotEvents}
        onClose={onCloseInvitor}
      />
    </View>
  )
}

export default HomeAllPopUp
